/**
 * @description 通用的路由跳转文件
 */
const routerPath = {
  'index': '/pages/index/index',
  'pay': '/pages/pay/index',
  'detail': '/pages/detail/detail'
}
module.exports = {
  // 页面跳转  
  // this.$router.push('/detail', {query: })
  // this.$router.push({ path: '', query: {} })
  push(path, option = {}) {
    if (typeof path == 'string') {
      option.path = path;
    } else {
      option = path;
    }
    // option = { path: '',  }
    let url = routerPath[option.path]; // /datetil?name=1&age=1
    let { query = {}, openType, duration } = option;

    let params = this.parse(query);

    if (params) {
      url += '?' + params;
    }
    duration ? setTimeout(() => {
      this.to(openType, url);
    }, duration) : this.to(openType, url);
  },
  to(openType, url) {
    let obj = { url };
    if (openType == 'redirect') {
      wx.redirectTo(obj) // 重定向
    } else if (openType == 'reLaunch') {
      wx.reLaunch(obj) // 
    } else if (openType == 'back') {
      wx.navigateBack({ // 
        delta: 1
      })
    } else {
      wx.navigateTo(obj); // 
    }
  },
  // 转义方法
  parse(data) {
    let arr = [];
    for (let key in data) {
      arr.push(key + '=' + data[key]);
    }
    // name=123&age=1
    return arr.join('&');
  }
}